package com.anjie.powerproject.service.impl;

import com.anjie.powerproject.entity.RelationshipCom;
import com.anjie.powerproject.pojo.CustomEdgeMulti;
import com.anjie.powerproject.pojo.CustomRelationshipCom;
import com.anjie.powerproject.repository.*;
import com.anjie.powerproject.service.RelationshipComService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RelationshipComServiceImpl implements RelationshipComService {
    @Autowired
    private RelationshipRepository relationshipRepository;
    @Autowired
    private DeviceRepository deviceRepository;
    @Autowired
    private EnergyTypeRepository energyTypeRepository;
    @Autowired
    private GraphInfoRepository graphInfoRepository;
    @Autowired
    private RelationshipComRepository relationshipComRepository;

    @Override
    public void saveRelationshipComList(List list) {
        relationshipComRepository.saveAll(list);
    }

    @Override
    public List findAllRelationshipCom() {
        return relationshipComRepository.findAll();
    }

    @Override
    public List findRelationshipComById(Integer id) {
        List<RelationshipCom> relationshipComs = relationshipComRepository.findRelationshipComsById(id);
        return relationshipComs;
    }

    @Override
    public List findRelationshipComByStgyCode(Integer stgyCode) {
        List<RelationshipCom> relationshipComs = relationshipComRepository.findRelationshipComsByStgyCode(stgyCode);
        return relationshipComs;
    }

    @Override
    public void deleteRelationshipComByIdAndStgyCode(Integer id, Integer stgyCode) {
        relationshipComRepository.deleteRelationshipComsByIdAndStgyCode(id, stgyCode);
    }

    @Override
    public List findCustomRelationshipComList() {
        List<RelationshipCom> list = relationshipComRepository.findAll();
        List<CustomRelationshipCom> res = new ArrayList<>();
        for (RelationshipCom relationshipCom : list) {
            CustomRelationshipCom object = new CustomRelationshipCom();
            Integer deviceFrom = relationshipCom.getDeviceFrom();
            Integer deviceTo = relationshipCom.getDeviceTo();
//            int typeId = relationshipCom.getTypeId();
            Integer graphId = relationshipCom.getGraphId();
            object.setId(relationshipCom.getId());
            object.setDeviceFrom(deviceFrom);
            object.setDeviceTo(deviceTo);
            object.setDeviceFromName(deviceRepository.selectCustomDevice(deviceFrom).getDeviceName());
            object.setDeviceToName(deviceRepository.selectCustomDevice(deviceTo).getDeviceName());
//            object.setLossRate(relationshipCom.getLossRate());
//            object.setTypeId(typeId);
//            object.setTypeName(energyTypeRepository.findById(typeId).get().getTypeName());
            object.setGraphId(graphId);
            object.setGraphName(graphInfoRepository.findById(graphId).get().getGraphName());
            object.setStgyCode(relationshipCom.getStgyCode());
            object.setRelName(relationshipCom.getRelName());
            object.setWorkTime(relationshipCom.getWorkTime());
            res.add(object);
        }
        return res;
    }

    @Override
    public RelationshipCom updateRelationshipCom(RelationshipCom relationshipCom) {
        RelationshipCom save = relationshipComRepository.save(relationshipCom);
        return relationshipCom;
    }

    @Override
    public void deleteRelationshipComById(Integer id) {
        relationshipComRepository.deleteById(id);
    }

    @Override
    public void deleteRelationshipComByStgyCode(Integer stgyCode) {
        relationshipComRepository.deleteAllByStgyCode(stgyCode);
    }

    @Override
    public List findRelationshipComByDeviceFromAndToAndStgyCode(Integer deviceFrom, Integer deviceTo, Integer stgyCode) {
        List list = relationshipComRepository.findRelationshipComsByDeviceFromAndDeviceToAndStgyCode(deviceFrom, deviceTo, stgyCode);
        return list;
    }

    @Override
    public List findRelationshipComByDeviceTo(Integer deviceTo) {
        List<RelationshipCom> list = relationshipComRepository.findRelationshipComByDeviceTo(deviceTo);
        return list;
    }

    @Override
    public void deleteRelationshipComByDeviceFromAndToAndStgyCode(Integer deviceFrom, Integer deviceTo, Integer stgyCode) {
        relationshipComRepository.deleteRelationshipComsByDeviceFromAndDeviceToAndStgyCode(deviceFrom, deviceTo, stgyCode);
    }

    @Override
    public List findCustomRelationshipComListByStgyCode(Integer stgyCode) {
        List<RelationshipCom> list = relationshipComRepository.findRelationshipComsByStgyCode(stgyCode);
        List<CustomRelationshipCom> res = new ArrayList<>();
        for (RelationshipCom relationshipCom : list) {
            CustomRelationshipCom object = new CustomRelationshipCom();
            Integer deviceFrom = relationshipCom.getDeviceFrom();
            Integer deviceTo = relationshipCom.getDeviceTo();
//            int typeId = relationshipCom.getTypeId();
            Integer graphId = relationshipCom.getGraphId();
            object.setId(relationshipCom.getId());
            object.setDeviceFrom(deviceFrom);
            object.setDeviceTo(deviceTo);
            object.setDeviceFromName(deviceRepository.selectCustomDevice(deviceFrom).getDeviceName());
            object.setDeviceToName(deviceRepository.selectCustomDevice(deviceTo).getDeviceName());
//            object.setLossRate(relationshipCom.getLossRate());
//            object.setTypeId(typeId);
//            object.setTypeName(energyTypeRepository.findById(typeId).get().getTypeName());
            object.setGraphId(graphId);
            object.setGraphName(graphInfoRepository.findById(graphId).get().getGraphName());
            object.setStgyCode(relationshipCom.getStgyCode());
            object.setRelName(relationshipCom.getRelName());
            object.setWorkTime(relationshipCom.getWorkTime());
            res.add(object);
        }
        return res;
    }

    @Override
    public List findAllStgyCode() {
        Integer[] stgyCodes = new Integer[1000];
        List<RelationshipCom> relationshipComList = relationshipComRepository.findAll();
        List<Integer> list = new ArrayList<>();
        int i,j;
        Integer temp;
        int count = 0;
        for (i = 0; i < relationshipComList.size(); i++) {
            temp = relationshipComList.get(i).getStgyCode();
            if (i == 0) {
                stgyCodes[count] = temp;
                count++;
            } else {
                if (!Arrays.asList(stgyCodes).contains(temp)) {
                    stgyCodes[count] = temp;
                    count++;
                }
            }
        }
        for(j=0;j<count;j++){
            list.add(stgyCodes[j]);
        }
        return list;
    }

    /**
     * 通过组合的形式展示
     * @param customRelationshipComList
     * @return
     */
    @Override
    public List findCustomEdgeMulti(List<CustomRelationshipCom> customRelationshipComList) {
        List<CustomEdgeMulti> customEdgeMultiList = new ArrayList<>();
        CustomEdgeMulti customEdgeMulti = new CustomEdgeMulti();
        List<Integer> id = new ArrayList<>();
        List<Integer> deviceFrom = new ArrayList<>();
        List<String> deviceFromName = new ArrayList<>();
//        List<Integer> typeId = new ArrayList<>();
//        List<String> typeName = new ArrayList<>();
        List<Integer> graphId = new ArrayList<>();
        List<String> graphName = new ArrayList<>();
//        List<Float> lossRate = new ArrayList<>();
        List<String> relName = new ArrayList<>();
        for(CustomRelationshipCom customRelationshipCom : customRelationshipComList){
            id.add(customRelationshipCom.getId());
            deviceFrom.add(customRelationshipCom.getDeviceFrom());
            deviceFromName.add(customRelationshipCom.getDeviceFromName());
//            typeId.add(customRelationshipCom.getTypeId());
//            typeName.add(customRelationshipCom.getTypeName());
            graphId.add(customRelationshipCom.getGraphId());
            graphName.add(customRelationshipCom.getGraphName());
//            lossRate.add(customRelationshipCom.getLossRate());
            relName.add(customRelationshipCom.getRelName());
        }
        customEdgeMulti.setDeviceTo(customRelationshipComList.get(0).getDeviceTo());
        customEdgeMulti.setDeviceToName(customRelationshipComList.get(0).getDeviceToName());
        customEdgeMulti.setStgyCode(customRelationshipComList.get(0).getStgyCode());
        customEdgeMulti.setWorkTime(customRelationshipComList.get(0).getWorkTime());
        customEdgeMulti.setId(id);
        customEdgeMulti.setDeviceFrom(deviceFrom);
        customEdgeMulti.setDeviceFromName(deviceFromName);
//        customEdgeMulti.setTypeId(typeId);
//        customEdgeMulti.setTypeName(typeName);
        customEdgeMulti.setGraphId(graphId);
        customEdgeMulti.setGraphName(graphName);
//        customEdgeMulti.setLossRate(lossRate);
        customEdgeMulti.setRelName(relName);
        customEdgeMultiList.add(customEdgeMulti);
        return customEdgeMultiList;
    }

    /**
     * 返回指定图谱下所有存在得组合策略编码
     * @param graphId
     * @return
     */
    @Override
    public List findAllStgyCodeByGraph(Integer graphId) {
        Integer[] stgyCodes = new Integer[1000];
        List<RelationshipCom> relationshipComList = relationshipComRepository.findRelationshipComsByGraphId(graphId);
        List<Integer> list = new ArrayList<>();
        int i,j;
        Integer temp;
        int count = 0;
        for (i = 0; i < relationshipComList.size(); i++) {
            temp = relationshipComList.get(i).getStgyCode();
            if (i == 0) {
                stgyCodes[count] = temp;
                count++;
            } else {
                if (!Arrays.asList(stgyCodes).contains(temp)) {
                    stgyCodes[count] = temp;
                    count++;
                }
            }
        }
        for(j=0;j<count;j++){
            list.add(stgyCodes[j]);
        }
        return list;
    }

    @Override
    public List findCustomRelationshipComListByGraphId(Integer graphId) {
        List<RelationshipCom> list = relationshipComRepository.findRelationshipComsByGraphId(graphId);
        List<CustomRelationshipCom> res = new ArrayList<>();
        for (RelationshipCom relationshipCom : list) {
            CustomRelationshipCom object = new CustomRelationshipCom();
            Integer deviceFrom = relationshipCom.getDeviceFrom();
            Integer deviceTo = relationshipCom.getDeviceTo();
//            int typeId = relationshipCom.getTypeId();
            object.setId(relationshipCom.getId());
            object.setDeviceFrom(deviceFrom);
            object.setDeviceTo(deviceTo);
            object.setDeviceFromName(deviceRepository.selectCustomDevice(deviceFrom).getDeviceName());
            object.setDeviceToName(deviceRepository.selectCustomDevice(deviceTo).getDeviceName());
//            object.setLossRate(relationshipCom.getLossRate());
//            object.setTypeId(typeId);
//            object.setTypeName(energyTypeRepository.findById(typeId).get().getTypeName());
            object.setGraphId(graphId);
            object.setGraphName(graphInfoRepository.findById(graphId).get().getGraphName());
            object.setStgyCode(relationshipCom.getStgyCode());
            object.setRelName(relationshipCom.getRelName());
            object.setWorkTime(relationshipCom.getWorkTime());
            res.add(object);
        }
        return res;
    }

    @Override
    public List findDeviceToListByGrraphId(Integer graphId) {
        List<Integer> list = relationshipRepository.findDeviceTosByGraphIdDistinct(graphId);
  /*      List DeviceList = new ArrayList();
        for(Relationship relationship : list){
            DeviceList.add(relationship.getDeviceTo());
        }*/
        return list;
    }

    @Override
    public boolean findRelationshipComsByDeviceFromAndDeviceTo(Integer deviceFrom,Integer deviceTo,Integer graphId){
        List<RelationshipCom> relationshipComs = relationshipComRepository.findRelationshipComsByDeviceFromAndDeviceToAndGraphId(deviceFrom,deviceTo,graphId);
        return relationshipComs==null||relationshipComs.size()==0?false:true;
    }

    @Override
    public Integer getMaxStgyCode() {
        return relationshipComRepository.findMaxStgyCode();
    }

    @Override
    public void deleteRelationshipsByGraphId(Integer graphId) {
        relationshipComRepository.deleteRelationshipComsByGraphId(graphId);
    }

    @Override
    /**
     * 重复返回true，不重复返回false
     */
    public boolean checkStgyRepeat(List<RelationshipCom> addList) {
        //找到与修改策略目标设备相同的策略
        List<RelationshipCom> relationshipComList = findRelationshipComByDeviceTo(addList.get(0).getDeviceTo());
        if (relationshipComList.size() != 0) {
            if (!relationshipComList.get(0).getWorkTime().equals(addList.get(0).getWorkTime())) {
                //工作时间修改了
                return false;
            }
            if (addList.size() != relationshipComList.size()) {
                return  false;
            }

            //使用set来判断源设备的情况
            Set<Integer> set = new HashSet<>();
            for (RelationshipCom relationshipCom : relationshipComList) {
                set.add(relationshipCom.getDeviceFrom());
            }

            for (RelationshipCom relationshipCom : addList) {
                if (!set.contains(relationshipCom.getDeviceFrom())) {
                    return false;
                }
            }

        }

        return true;
    }
}
